﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;

namespace TMDT.Web.UI.Controls
{
    public partial class ChiTietSanPhamControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    LoadData();
                }
                else
                {
                    Response.Redirect("SanPham.aspx");
                }
            }
        }

        private void LoadData()
        {
            LoadChiTiet();
            LoadThongTinKyThuat();
        }

        private void LoadThongTinKyThuat()
        {
            var service = new NoiDungSanPhamManager();
            dtlThongTinKyThuat.DataSource = service.GetNoiDungByIDSanPham(new Guid(Request["ID"]));
            dtlThongTinKyThuat.DataBind();
        }

        private void LoadChiTiet()
        {
            var service = new SanPhamManager();
            var sanPham = service.GetSanPham(Request["ID"]);
            lblBaoHanh.Text = sanPham.ThoiGianBaoHanh.ToString() + " tháng";
            lblGia.Text = String.Format("{0:0,0 vnđ}", sanPham.Gia);
            lblLuotXem.Text = sanPham.SoLuotXem.ToString();
            lblNgayDang.Text = sanPham.NgayTao.ToShortDateString();
            lblNhaSanXuat.Text = new NhaSanXuatManager().GetNhaSanXuat(sanPham.IDNhaSanXuat).TenNhaSanXuat;
            lblSoLuong.Text = sanPham.SoLuong.ToString();
            lblTenSanPham.Text = sanPham.TenSanPham;

            var hinhAnh = new HinhAnhManager();
            imgHinh.ImageUrl = hinhAnh.GetHinhAnhByIDSanPham(new Guid(Request["ID"])).URL;
        }

        protected void dtlThongTinKyThuat_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            NoiDungSanPham noiDung = (NoiDungSanPham) e.Item.DataItem;

            var lblTenThuocTinh = e.Item.FindControl("lblTenThuocTinh") as Label;
            lblTenThuocTinh.Text = new ThuocTinhManager().GetThuocTinh(noiDung.IDThuocTinh).TenThuocTinh;
        }
    }
}